home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Vertigo / RenderEngine / DrawingUtils.cp < prev    next >
Encoding:
Text File  |  2000-06-24  |  4.9 KB  |  189 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    File:        DrawingUtils.cp
  4. //
  5. //    Project:    MacHack 2000 - Vertigo!
  6. //    Authors:    Darrin Cardani, Drew Thaler, Ed Wynne
  7. //
  8. //    Date:        06/23/2000 (written entirely during the conference!)
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11.  
  12. #define DISABLE_LOCAL_CALLTRACE            0
  13. #define DISABLE_LOCAL_DEBUG                0
  14. #include "DebugUtils.h"
  15.  
  16. #include "DrawingUtils.h"
  17.  
  18.  
  19.  
  20. // ---------------------------------------------------------------------------
  21. //    RegionList
  22. // ---------------------------------------------------------------------------
  23. RegionList::RegionList()
  24. {
  25.     mArrayStorage = NewHandleSys(8);
  26.     dAssert(mArrayStorage != NULL);
  27.     HLock(mArrayStorage);
  28.     mArray = (*(RgnHandle**)mArrayStorage);
  29.     
  30.     mNull = NULL;
  31.     mItemsInArray = 0;
  32.     mCurrentArrayCapacity = 2;
  33. }
  34.  
  35. RegionList::~RegionList()
  36. {
  37.     if (mArrayStorage != NULL) DisposeHandle(mArrayStorage); mArrayStorage = NULL;
  38.     mItemsInArray = 0;
  39.     mCurrentArrayCapacity = 0;
  40. }
  41.  
  42. // ---------------------------------------------------------------------------
  43. //    RegionList::Add
  44. // ---------------------------------------------------------------------------
  45.  
  46. void
  47. RegionList::Add(RgnHandle rgn, int duplicateRgn)
  48. {
  49.     dAssert(mArrayStorage != NULL);
  50.     dAssert(rgn != NULL);
  51.     if ((mArrayStorage == NULL) || (rgn == NULL))
  52.         return;
  53.     
  54.     // Duplicate the region if we've been told.
  55.     if (duplicateRgn)
  56.     {
  57.         RgnHandle    tempRgn = NewRgn();
  58.         CopyRgn(rgn,tempRgn);
  59.         rgn = tempRgn;
  60.     }
  61.     
  62.     // Do the easy case, just slam into the end of the array.
  63.     if (mItemsInArray < mCurrentArrayCapacity)
  64.     {
  65.         mArray[mItemsInArray++] = rgn;
  66.         return;
  67.     }
  68.     
  69.     // Grow the handle since we need more space.
  70.     HUnlock(mArrayStorage);
  71.     SetHandleSize(mArrayStorage,2*sizeof(RgnHandle)*mCurrentArrayCapacity);
  72.     HLock(mArrayStorage);
  73.     
  74.     // Sanity check to make sure the handle really grew.
  75.     mArray = (*(RgnHandle**)mArrayStorage);
  76.     mCurrentArrayCapacity = GetHandleSize(mArrayStorage) / sizeof(RgnHandle);
  77.     
  78.     dAssert(mItemsInArray < mCurrentArrayCapacity);
  79.     if (mItemsInArray < mCurrentArrayCapacity)
  80.         mArray[mItemsInArray++] = rgn;
  81.     else
  82.         dprintf(kDConPrefix "RegionList::Add - SetHandleSize failed!  MemErr = %ld\n", (long)MemError());
  83. }
  84.  
  85.  
  86. // ---------------------------------------------------------------------------
  87. //    RegionList::Remove
  88. // ---------------------------------------------------------------------------
  89.  
  90. void
  91. RegionList::Remove(RgnHandle rgn, int dispose)
  92. {
  93.     dAssert(mArrayStorage != NULL);
  94.     dAssert(rgn != NULL);
  95.     if ((mArrayStorage == NULL) || (rgn == NULL))
  96.         return;
  97.     
  98.     // Walk through the list looking for the region.
  99.     for (int i=0; i<mItemsInArray; ++i)
  100.     {
  101.         if ((rgn == mArray[i]) || (EqualRgn(rgn,mArray[i])))
  102.         {
  103.             // Got it.  If we're disposing then do it now.
  104.             if (dispose) DisposeRgn(mArray[i]);
  105.             
  106.             // Shift the array down.
  107.             mItemsInArray--;
  108.             BlockMoveData(&mArray[i+1],&mArray[i],(mItemsInArray-i)*sizeof(RgnHandle));
  109.             return;
  110.         }
  111.     }
  112.     
  113.     // Didn't find it.  print out a message for debugging and return.
  114.     dprintf(kDConPrefix "RegionList::Remove - rgn $%08X is not in list!\n", (int)rgn);
  115. }
  116.  
  117.  
  118. // ---------------------------------------------------------------------------
  119. //    RegionList::RemoveAll
  120. // ---------------------------------------------------------------------------
  121.  
  122. void
  123. RegionList::RemoveAll(int dispose)
  124. {
  125.     if (dispose)
  126.         for (int i=0; i<mItemsInArray; ++i)
  127.             DisposeRgn(mArray[i]);
  128.     
  129.     mItemsInArray = 0;
  130. }
  131.  
  132.  
  133. // ---------------------------------------------------------------------------
  134. //    RegionList::operator[]
  135. // ---------------------------------------------------------------------------
  136.  
  137. RgnHandle &
  138. RegionList::operator[] (UInt32 index)
  139. {
  140.     mNull = NULL;
  141.     dAssert(mArrayStorage != NULL);
  142.     if (mArrayStorage == NULL)
  143.         return mNull;
  144.     
  145.     // Grow the array in response to invalid indices.  This
  146.     //    is deliberate, so that we don't have to worry about hitting
  147.     //    a bad index when re-ordering windows in the list.
  148.     while (index > mCurrentArrayCapacity)
  149.     {
  150.         dprintf(kDConPrefix "Growing array to %ld items\n", 2*mCurrentArrayCapacity);
  151.         
  152.         HUnlock(mArrayStorage);
  153.         SetHandleSize(mArrayStorage,2*sizeof(RgnHandle)*mCurrentArrayCapacity);
  154.         HLock(mArrayStorage);
  155.         if (MemError()) return mNull;
  156.         
  157.         mArray = (*(RgnHandle**)mArrayStorage);
  158.         mCurrentArrayCapacity = GetHandleSize(mArrayStorage) / sizeof(RgnHandle);
  159.     }
  160.     
  161.     // Okay, return it.
  162.     return mArray[index];
  163. }
  164.  
  165.  
  166.  
  167. #pragma mark -
  168.  
  169.  
  170.  
  171. // ---------------------------------------------------------------------------
  172. //    StGWorld::Create(Rect)
  173. // ---------------------------------------------------------------------------
  174. //    Why would you ever want anything but a 32-bit GWorld?
  175. //
  176. void
  177. StGWorld::Create(const Rect &inRect)
  178. {
  179.     OSStatus    err = NewGWorld(&mGWorld,32,&inRect,NULL,NULL,useTempMem | keepLocal | pixelsLocked);
  180.     if (err != noErr) {
  181.         dprintf(kDConPrefix "GWorld allocation (%dx%d) failed!  err = %ld\n",
  182.             (inRect.right - inRect.left), (inRect.bottom - inRect.top), err);
  183.         mGWorld = NULL;
  184.         return;
  185.     }
  186. }
  187.  
  188.  
  189.